export default {
  data() {
    return {
      loansCount: '', //公积金贷款总额
      businessCount: '', //商业贷款总额
      yourRate: '', //自定义公积金利率
      yourBusiness: '', //自定义商业利率
      rateShow: false, //监听自定义公积金利率显示
      yourBusinessShow: false, //监听自定义商业利率显示

      //公积金贷款利率
      rateIndex: -1,
      loansRate: [
        {
          type: 0,
          name: '首套1-5年(含)  (2.6%)',
          rate: 0.026
        },
        {
          type: 1,
          name: '首套5年以上  (3.1%)',
          rate: 0.031
        },
        {
          type: 2,
          name: '二套1-5年(含)  (3.025%)',
          rate: 0.03025
        },
        {
          type: 3,
          name: '二套5年以上  (3.575%)',
          rate: 0.03575
        },
        {
          type: 4,
          name: '其他利率  (可输入)'
        }
      ],

      fundShow: true,
      businessShow: false,

      //商业贷款利率
      businessIndex: -1,
      businessRate: [
        {
          type: 0,
          name: '首套5年以上基准利率(4.900%)',
          rate: 0.049
        },
        {
          type: 1,
          name: '二套5年以上最低利率(5.390%)',
          rate: 0.0539
        },
        {
          type: 2,
          name: '其他利率(可输入)'
        }
      ],

      //还款方式
      refundIndex: -1,
      refundWay: [{
        type: 0,
        name: '等额本息'
      },
      {
        type: 1,
        name: '等额本金'
      }
      ],

      //按揭期数
      yearIndex: -1,
      mortgageYear: [{
        type: 0,
        name: '1年(12期)',
        num: 12
      },
      {
        type: 1,
        name: '2年(24期)',
        num: 24
      },
      {
        type: 2,
        name: '3年(36期)',
        num: 36
      },
      {
        type: 3,
        name: '4年(48期)',
        num: 48
      },
      {
        type: 4,
        name: '5年(60期)',
        num: 60
      },
      {
        type: 5,
        name: '6年(72期)',
        num: 72
      },
      {
        type: 6,
        name: '7年(84期)',
        num: 84
      },
      {
        type: 7,
        name: '8年(96期)',
        num: 96
      },
      {
        type: 8,
        name: '9年(108期)',
        num: 108
      },
      {
        type: 9,
        name: '10年(120期)',
        num: 120
      },
      {
        type: 10,
        name: '11年(132期)',
        num: 132
      },
      {
        type: 11,
        name: '12年(144期)',
        num: 144
      },
      {
        type: 12,
        name: '13年(156期)',
        num: 156
      },
      {
        type: 13,
        name: '14年(168期)',
        num: 168
      },
      {
        type: 14,
        name: '15年(180期)',
        num: 180
      },
      {
        type: 15,
        name: '16年(192期)',
        num: 192
      },
      {
        type: 16,
        name: '17年(204期)',
        num: 204
      },
      {
        type: 17,
        name: '18年(216期)',
        num: 216
      },
      {
        type: 18,
        name: '19年(228期)',
        num: 228
      },
      {
        type: 19,
        name: '20年(240期)',
        num: 240
      },
      {
        type: 20,
        name: '21年(252期)',
        num: 252
      },
      {
        type: 21,
        name: '22年(264期)',
        num: 264
      },
      {
        type: 22,
        name: '23年(276期)',
        num: 276
      },
      {
        type: 23,
        name: '24年(288期)',
        num: 288
      },
      {
        type: 24,
        name: '25年(300期)',
        num: 300
      },
      {
        type: 25,
        name: '26年(312期)',
        num: 312
      },
      {
        type: 26,
        name: '27年(324期)',
        num: 324
      },
      {
        type: 27,
        name: '28年(336期)',
        num: 336
      },
      {
        type: 28,
        name: '29年(348期)',
        num: 348
      },
      {
        type: 29,
        name: '30年(360期)',
        num: 360
      }

      ],

      //tab栏选项
      tnavList: [
        {
          id: 1,
          name: '公积金贷款',

        },
        {
          id: 2,
          name: '商业贷款',
        },
        {
          id: 3,
          name: '组合贷款',
        }
      ],
      current: 0, //一级导航序号,

      actualRate: null, //实际选择的公积金利率
      actualBusiness: null, //实际选择的商业利率
      actualYear: null, //实际选择的期数

      homePrice: null,//还款月供

      resultShow: false,

      refundType: null,

      menuType: 1,
      activeStyle: {
        color: '#1C87F3',
        fontSize: '32rpx'
      },
      inactiveStyle: {
        color: '#333333',
        fontSize: '32rpx'
      },
      itemStyle: {
        height: '100rpx', background: '#fff'
      }
    }
  },
  watch: {
    'rateIndex'(e) {
      if (e == 4) {
        this.rateShow = true
      } else {
        this.rateShow = false
      }
    },

    'businessIndex'(e) {
      if (e == 2) {
        this.yourBusinessShow = true
      } else {
        this.yourBusinessShow = false
      }
    }
  },
  methods: {
    //导航栏回调事件
    menuClick(e) {
      this.menuType = e.id
      if (e.id == 1) {
        this.loansCount = ''
        this.businessCount = ''
        this.yourRate = ''
        this.yourBusiness = ''
        this.refundIndex = -1
        this.rateIndex = -1
        this.yearIndex = -1
        this.businessIndex = -1
        this.fundShow = true
        this.businessShow = false
        this.rateShow = false
        this.yourBusinessShow = false
        this.resultShow = false
      } else if (e.id == 2) {
        this.loansCount = ''
        this.businessCount = ''
        this.yourRate = ''
        this.yourBusiness = ''
        this.refundIndex = -1
        this.rateIndex = -1
        this.yearIndex = -1
        this.businessIndex = -1
        this.fundShow = false
        this.businessShow = true
        this.rateShow = false
        this.yourBusinessShow = false
        this.resultShow = false
      } else if (e.id == 3) {
        this.loansCount = ''
        this.businessCount = ''
        this.yourRate = ''
        this.yourBusiness = ''
        this.refundIndex = -1
        this.rateIndex = -1
        this.yearIndex = -1
        this.businessIndex = -1
        this.fundShow = true
        this.businessShow = true
        this.rateShow = false
        this.yourBusinessShow = false
        this.resultShow = false
      }
    },

    //公积金利率计算方式
    rateChange(e) {
      this.rateIndex = e.detail.value
      if (e.detail.value != 4) {
        this.actualRate = this.loansRate[e.detail.value].rate / 12
      }
    },

    //商业利率计算方式
    businessChange(e) {
      this.businessIndex = e.detail.value
      if (e.detail.value != 2) {
        this.actualBusiness = this.businessRate[e.detail.value].rate / 12
      }
    },

    //还款方式选择
    refundChange(e) {
      this.refundIndex = e.detail.value
    },

    //按揭年数选择
    yearChange(e) {
      this.yearIndex = e.detail.value
      this.actualYear = this.mortgageYear[e.detail.value].num
    },

    //重置
    resetBtn() {
      this.loansCount = ''
      this.businessCount = ''
      this.yourRate = ''
      this.yourBusiness = ''
      this.refundIndex = -1
      this.rateIndex = -1
      this.yearIndex = -1
      this.businessIndex = -1
      this.rateShow = false
      this.yourBusinessShow = false
      this.resultShow = false
    },

    //计算
    countBtn() {
      this.refundType = this.refundIndex
      if (this.menuType == 1) {
        if (this.loansCount == '') {
          this.$tools.msg('请填写贷款总额')
          return
        }
        if (this.rateIndex == -1) {
          this.$tools.msg('请选择贷款利率')
          return
        } else if (this.rateIndex == 4 && this.yourRate == '') {
          this.$tools.msg('请填写贷款利率')
          return
        }
        if (this.yearIndex == -1) {
          this.$tools.msg('请选择按揭期数')
          return
        }
        if (this.refundIndex == -1) {
          this.$tools.msg('请选择还款方式')
          return
        }

        var actualRate;
        if (this.rateIndex == 4) {
          actualRate = this.yourRate / 100 / 12
          console.log('这里是自定义利率')
        } else {
          actualRate = this.actualRate
          console.log('这里是固定利率')
        }

        if (this.refundIndex == 0) {
          const rate = this.loansCount * 10000 * actualRate * Math.pow((1 + actualRate), this.actualYear) / (Math.pow((1 + actualRate), this.actualYear) - 1)
          this.homePrice = rate.toFixed(2)
          this.resultShow = true
        } else {
          const rate = (this.loansCount * 10000 / this.actualYear) + (this.loansCount * 10000) * actualRate
          this.homePrice = rate.toFixed(2)
          this.resultShow = true
        }
      } else if (this.menuType == 2) {
        if (this.businessCount == '') {
          this.$tools.msg('请填写贷款总额')
          return
        }
        if (this.businessIndex == -1) {
          this.$tools.msg('请选择贷款利率')
          return
        } else if (this.businessIndex == 2 && this.yourBusiness == '') {
          this.$tools.msg('请填写贷款利率')
          return
        }
        if (this.yearIndex == -1) {
          this.$tools.msg('请选择按揭期数')
          return
        }
        if (this.refundIndex == -1) {
          this.$tools.msg('请选择还款方式')
          return
        }
        var actualRate;
        if (this.businessIndex == 2) {
          actualRate = this.yourBusiness / 100 / 12
          console.log('这里是自定义利率')
        } else {
          actualRate = this.actualBusiness
          console.log('这里是固定利率')
        }
        if (this.refundIndex == 0) {
          const rate = this.businessCount * 10000 * actualRate * Math.pow((1 + actualRate), this.actualYear) / (Math.pow((1 + actualRate), this.actualYear) - 1)
          this.homePrice = rate.toFixed(2)
          this.resultShow = true
        } else {
          const rate = (this.businessCount * 10000 / this.actualYear) + (this.businessCount * 10000) * actualRate
          this.homePrice = rate.toFixed(2)
          this.resultShow = true
        }
      } else if (this.menuType == 3) {
        if (this.loansCount == '') {
          this.$tools.msg('请填写公积金贷款总额')
          return
        }
        if (this.rateIndex == -1) {
          this.$tools.msg('请选择贷款利率')
          return
        } else if (this.rateIndex == 4 && this.yourRate == '') {
          this.$tools.msg('请填写贷款利率')
          return
        }
        if (this.businessCount == '') {
          this.$tools.msg('请填写商业贷款总额')
          return
        }
        if (this.businessIndex == -1) {
          this.$tools.msg('请选择贷款利率')
          return
        } else if (this.businessIndex == 2 && this.yourBusiness == '') {
          this.$tools.msg('请填写贷款利率')
          return
        }
        if (this.yearIndex == -1) {
          this.$tools.msg('请选择按揭期数')
          return
        }
        if (this.refundIndex == -1) {
          this.$tools.msg('请选择还款方式')
          return
        }

        var actualRate;
        if (this.rateIndex == 4) {
          actualRate = this.yourRate / 100 / 12
          console.log('这里是自定义利率')
        } else {
          actualRate = this.actualRate
          console.log('这里是固定利率')
        }
        var actualRate2;
        if (this.businessIndex == 2) {
          actualRate2 = this.yourBusiness / 100 / 12
          console.log('这里是自定义利率')
        } else {
          actualRate2 = this.actualBusiness
          console.log('这里是固定利率')
        }

        if (this.refundIndex == 0) {
          const rate = this.loansCount * 10000 * actualRate * Math.pow((1 + actualRate), this.actualYear) / (Math.pow((1 + actualRate), this.actualYear) - 1)
          const rate2 = this.businessCount * 10000 * actualRate2 * Math.pow((1 + actualRate2), this.actualYear) / (Math.pow((1 + actualRate2), this.actualYear) - 1)
          this.homePrice = (rate + rate2).toFixed(2)
          this.resultShow = true
        } else {
          const rate = (this.loansCount * 10000 / this.actualYear) + (this.loansCount * 10000) * actualRate
          const rate2 = (this.businessCount * 10000 / this.actualYear) + (this.businessCount * 10000) * actualRate2
          this.homePrice = (rate + rate2).toFixed(2)
          this.resultShow = true
        }
      }

    }
  }
}